<template>
  <div class="sxfw-main-content">
    <el-row class="context-breadcrumb" v-if="breOpen && breLayout == 'content'">
      <breadcrumb></breadcrumb>
    </el-row>
    <!-- 主入口标签页 s -->

    <br v-if="breOpen && breLayout == 'content'" />

    <!--   siteContentViewHeight： {{siteContentViewHeight}} -->

    <el-row v-if="tabOpen">
      <el-tabs
        class="sxfw-context"
        v-model="mainTabsActiveName"
        @tab-click="selectedTabHandle"
        @tab-remove="removeTabHandle"
      >
        <el-tab-pane
          v-for="item in mainTabs"
          :key="item.name"
          :label="item.title"
          :name="item.name"
          :closable="item.name != 'home'"
          :class="item.name != 'home' ? 'context-home' : null"
        >
          <div :style="siteContentViewHeight">
            <iframe
              v-if="item.type === 'iframe'"
              :src="item.iframeUrl"
              width="100%"
              height="100%"
              frameborder="0"
              scrolling="yes"
            >
            </iframe>
            <keep-alive v-else>
              <router-view v-if="item.name === mainTabsActiveName" />
            </keep-alive>
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-row>
    <!-- 主入口标签页 e -->
    <main v-else :style="siteContentViewHeight">
      <keep-alive>
        <router-view :style="siteContentViewHeight" />
      </keep-alive>
    </main>
  </div>
</template>

<script>
import { isURL } from "@/utils/validate";
import breadcrumb from "@/components/breadcrumb";
export default {
  components: {
    breadcrumb
  },
  data() {
    return {};
  },
  computed: {
    documentClientHeight: {
      get() {
        return this.$store.state.common.documentClientHeight;
      }
    },
    menuActiveName: {
      get() {
        return this.$store.state.common.menuActiveName;
      },
      set(val) {
        this.$store.commit("common/updateMenuActiveName", val);
      }
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      }
    },
    mainTabsActiveName: {
      get() {
        return this.$store.state.common.mainTabsActiveName;
      },
      set(val) {
        this.$store.commit("common/updateMainTabsActiveName", val);
      }
    },
    breLayout: {
      get() {
        return this.$store.state.common.breadcrumbLayout;
      }
    },
    breOpen: {
      get() {
        return this.$store.state.common.breadcrumbOpen;
      }
    },
    tabOpen: {
      get() {
        return this.$store.state.common.tabOpen;
      }
    },
    siteContentViewHeight() {
      var height = this.documentClientHeight;
      // 减去header
      height -= 50;
      if (this.tabOpen) {
        height -= 50;
      }
      if (this.breOpen && this.breLayout == "content") {
        height -= 50;
      }

      return [
        { minHeight: height + "px" },
        { height: height + "px" },
        { overflow: "auto" }
      ];
    }
  },
  methods: {
    // tabs, 选中tab
    selectedTabHandle(tab) {
      tab = this.mainTabs.filter(item => item.name === tab.name);
      if (tab.length >= 1) {
        this.$router.push({ name: tab[0].name, query: tab[0].query });
      }
    },
    // tabs, 删除tab
    removeTabHandle(tabName) {
      this.mainTabs = this.mainTabs.filter(item => item.name !== tabName);
      if (this.mainTabs.length >= 1) {
        // 当前选中tab被删除
        if (tabName === this.mainTabsActiveName) {
          const tab = this.mainTabs[this.mainTabs.length - 1];
          this.$router.push({ name: tab.name, query: tab.query }, () => {
            this.mainTabsActiveName = this.$route.name;
          });
        }
      } else {
        this.menuActiveName = "";
        this.$router.push({ name: "home" });
      }
    },
    // tabs, 关闭当前
    tabsCloseCurrentHandle() {
      this.removeTabHandle(this.mainTabsActiveName);
    },
    // tabs, 关闭其它
    tabsCloseOtherHandle() {
      this.mainTabs = this.mainTabs.filter(
        item => item.name === this.mainTabsActiveName
      );
    },
    // tabs, 关闭全部
    tabsCloseAllHandle() {
      this.mainTabs = [];
      this.menuActiveName = "";
      this.$router.push({ name: "home" });
    },
    // tabs, 刷新当前
    tabsRefreshCurrentHandle() {
      var tempTabName = this.mainTabsActiveName;
      this.removeTabHandle(tempTabName);
      this.$nextTick(() => {
        this.$router.push({ name: tempTabName });
      });
    }
  }
};
</script>

<style type="text">
.el-tabs__nav-scroll {
  height: 39px;
  line-height: 34px;
}
.el-tabs__nav-wrap is-top {
  height: 34px !important;
  line-height: 34px !important;
}
.el-tabs__header {
  margin: 0 10px 3px;
}
</style>

<style lang="scss">
.sxfw-main-content {
  /* overflow-y: auto; */
  .context-breadcrumb {
    height: 40px;
    line-height: 40px;
    min-height: 40px;
    padding-left: 20px;
  }
  .el-card {
    overflow: auto;
  }

  .el-tabs__header:first-child {
    .el-tabs__nav {
      .el-tabs__item {
        box-sizing: border-box;
        display: inline-block;
        list-style: none;
        font-weight: 500;
        position: relative;
        display: inline-block;
        cursor: pointer;
        height: 26px;
        line-height: 26px;
        border: 1px solid #d8dce5;
        background: #fff;
        padding: 0 8px;
        font-size: 12px;
        margin-left: 5px;
        margin-top: 4px;
        text-align: center;

        .el-icon-close:before {
          transform: scale(0.7);
        }
      }
      .el-tabs__item:first-child,
      .el-tabs__item.is-top {
        margin-left: 10px;
        padding-left: 10px;
      }
      .el-tabs__item:last-child {
        margin-right: 15px;
        padding-right: 10px;
      }

      .el-tabs__item::before {
        content: "";
        background: #fff;
        display: inline-block;
        width: 8px;
        height: 8px;
        border-radius: 50%;
        position: relative;
        margin-right: 2px;
      }
      .el-tabs__item.is-active {
        /*color: #409EFF;*/
        background-color: #42b983;
        color: #fff;
        border-color: #42b983;
      }
      .el-tabs__active-bar {
        display: none;
      }
    }
  }

  .sxfw-context {
    /*float: left;*/
    .el-tabs__header {
      margin: 0 20px 3px;
    }

    .context-home {
      padding-right: 5px;
    }

    #tab-home {
      padding-right: 20px;
    }
  }
}
</style>
